package pab.ui.auxiliary;
import javax.swing.text.JTextComponent;
public abstract class AbstractAutoCompleteAdaptor {

    /** the string representation in use for the currently selected item*/
    private String selectedItemAsString;

    /**
     * Returns the currently selected item.
     * @return the selected item
     */
    public abstract Object getSelectedItem();

    /**
     * Sets the selected item.
     * @param item the item that is to be selected
     */
    public abstract void setSelectedItem(Object item);

    /**
     * Returns the string representation in use for the currently selected item.
     * @return the string representation in use for the currently selected item
     */
    public String getSelectedItemAsString() {
        return this .selectedItemAsString;
    }

    /**
     * Sets the string representation in use for the currently selected item.
     * @param itemAsString the string representation in use for the currently selected item
     */
    public void setSelectedItemAsString(String itemAsString) {
        this .selectedItemAsString = itemAsString;
    }

    /**
     * Returns the number of items in the list.
     * @return the number of items in the list
     */
    public abstract int getItemCount();

    /**
     * Returns the item at a given index. It is supposed that <code>0&lt;=index&lt;<b>getItemCount()</b></code>.
     * @param index the index of the item that is to be returned
     * @return the item at the given <code>index</code>
     */
    public abstract Object getItem(int index);

    /**
     * Returns true if the list contains the currently selected item.
     * @return true if the list contains the currently selected item.
     */
    public boolean listContainsSelectedItem() {
        Object selectedItem = getSelectedItem();
        for (int i = 0, n = getItemCount(); i < n; i++) {
            if (getItem(i) == selectedItem)
                return true;
        }
        return false;
    }

    /**
     * Returns the text component that is being used for the automatic completion.
     * @return the text component being used for the automatic completion
     */
    public abstract JTextComponent getTextComponent();

    /**
     * Marks/selects the entire text that is displayed inside the text component.
     */
    public void markEntireText() {
        markText(0);
    }

    /**
     * Marks/selects the text that is displayed inside the text component starting from the
     * character with index <tt>start</tt>.
     * @param start index of the first character that should be marked
     */
    public void markText(int start) {
        getTextComponent().setCaretPosition(
                getTextComponent().getText().length());
        getTextComponent().moveCaretPosition(start);
    }
}
